package cn.agiledata.bank.common.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import cn.agiledata.bank.common.util.DateUtil;
import cn.agiledata.bank.common.util.StringUtil;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.facade.helper.TemplateHelper;
import cn.agiledata.bank.hibernateBean.ScoresHis;
import cn.agiledata.bank.hibernateBean.SubAccount;
import cn.agiledata.bank.hibernateBean.Transaction;
import cn.agiledata.sms.SmsService;

import com.vnv.infrastructure.email.service.IMailService;
import com.vnv.infrastructure.email.service.VNVMail;

/*******************************************************************************
 * 不再继承SetUpUses类
 * 
 * @author 马贤波 
 * 2007-02-05
 */
public abstract class NotifyMail {
    protected final static Logger log = Logger.getLogger(NotifyMail.class);

    private String vmFile;// 邮件模板

    private String emailTitle;// 邮件标题

    private String contentTitle;// 邮件正文标题

    protected IMailService notificationService;// 邮件发送服务

    private String url;// 发送邮件的服务器

    private TemplateHelper template;

    private SmsService smsService;

    private String smsContext;
    
    private List attachment;

    public String getSmsContext() {
        return smsContext;
    }

    public void setSmsContext(String smsContext) {
        this.smsContext = smsContext;
    }

    public SmsService getSmsService() {
		return smsService;
	}

	public void setSmsService(SmsService smsService) {
		this.smsService = smsService;
	}

	public TemplateHelper getTemplate() {
        return template;
    }

    public void setTemplate(TemplateHelper template) {
        this.template = template;
    }

    public String getUrl() {
    	if(url != null && !url.endsWith("/") && url.indexOf(".") < 0) {
    		url += "/";
    	}
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
    
    public void sendSms(Transaction tran, ScoresHis score, String template,
			Map mapParam) {
    	Map vnvMap = new HashMap();
    	vnvMap.putAll(mapParam);
        SubAccount sendTo = setMailContext(vnvMap, tran, score);
        String mobile = sendTo.getAccount().getMobile();
        String text = StringUtil.appendWebSiteUrl(this.template.mergeContent(
        		template, vnvMap));
        try {
            this.smsService.sendMessage(mobile, text);
        } catch (Exception e) {
        	log.error(e);
        }
    }

    public void sendSms(Transaction tran, ScoresHis score) {
        Map vnvMap = new HashMap();
        SubAccount sendTo = setMailContext(vnvMap, tran, score);
        String mobile = sendTo.getAccount().getMobile();
        String text = StringUtil.appendWebSiteUrl(this.template.mergeContent(
				getSmsContext(), vnvMap));
        System.out.println(text+"*************************");
        try {
            this.smsService.sendMessage(mobile, text);
        } catch (Exception e) {
        	log.error(e);
        }
    }
    
    /**
     * 根据手机号码发送短信
     * 
     * @param mobileNum
     */
    public void sendSmsByParam(String mobileNum,String template,
			Map mapParam) {
    	String context = StringUtil.appendWebSiteUrl(this.template.mergeContent(
        		template, mapParam));
        try {
            this.smsService.sendMessage(mobileNum, context);
        } catch (Exception e) {
        	log.error(e);
        }
    }

    /**
     * 发送交易相关邮件
     * 
     * @param tran
     *            交易
     * @return void
     */
    public void sendMail(Transaction tran, ScoresHis score) {
        Map vnvMap = new HashMap();
        VNVMail mail = new VNVMail();
        /** 根据各自模板填入邮件元素* */
        SubAccount emailTo = setMailContext(vnvMap, tran, score);
        /** 收信方email* */
        mail.setEmailTo(emailTo.getAccount().getEmail());
		/** 收信方称呼 */
        vnvMap.put("name", WeatherUtil.getNameOrEmailOrMobileByAccount(emailTo.getAccount()));

        /** 邮件正文标题 */
        vnvMap.put("contentTitle", getContentTitle());
        /** 邮件签名发送时间 */
        vnvMap.put("sendTime", DateUtil.date2String(new Date()));
        /** 邮件标题* */
        mail.setSubject(getEmailTitle());
        /** 邮件内容* */
        mail.setContext(vnvMap);
        /** 设置模板 */
        mail.setTemplateName(getVmFile());
        mail.setAttachmentUrl(getAttachment());
        /** 邮件发送* */
        notificationService.sendMail(mail);
        log.debug("邮件发送完成...");
    }

    /**
     * 根据各自模板填入邮件元素
     */
    protected abstract SubAccount setMailContext(Map vnvMap, Transaction tran,
            ScoresHis score);

    public  List getAttachment() {
        return this.attachment;
    }
    
    public void setAttachment(List a){
    	this.attachment = a ;
    }
    /**
     * @return 交易编号的末尾6位
     */
    protected final String tailOfTransId(String transId) {
        if (transId == null || transId.length() < 6) {
            return null;
        }
        return transId.substring(transId.length() - 6);
    }

    public void setVmFile(String vmFile) {
        this.vmFile = vmFile;
    }

    public void setEmailTitle(String emailTitle) {
        this.emailTitle = emailTitle;
    }

    public void setNotificationService(IMailService notificationService) {
        this.notificationService = notificationService;
    }

    public String getEmailTitle() {
        return emailTitle;
    }

    public IMailService getNotificationService() {
        return notificationService;
    }

    public String getVmFile() {
        return vmFile;
    }

    public String getContentTitle() {
        return contentTitle;
    }

    public void setContentTitle(String contentTitle) {
        this.contentTitle = contentTitle;
    }

}
